//
//  peopleListVC.m
//  tcpoctest
//
//  Created by gump on 10/26/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "peopleListVC.h"
#import "PersonCell.h"
#import "NearPeopleInfo.h"

@interface peopleListVC ()

@end

@implementation peopleListVC

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"附近的人";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    NSString *strnexttitle = @"筛选";
    switch (getNearPeopleFilterType()) {
        case NTF_ALL:
        {
            strnexttitle = @"筛选";
        }
            break;
            case NTF_FEMALE:
        {
            strnexttitle = @"筛选(女)";
        }
            break;
        case NTF_MALE:
        {
            strnexttitle = @"筛选(男)";
        }
            break;
        default:
            break;
    }
    
    nextItem = [self createBarButtonItemWithTitle:strnexttitle
                                                            target:self
                                                            action:@selector(nextButtonPressed)];
    self.navItem.rightBarButtonItem = nextItem;
    
    mtable =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                                     style:UITableViewStylePlain];
    mtable.backgroundColor=CELL_BG_COLOR;
    mtable.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    mtable.delegate =self;
    mtable.dataSource =self;
    [self.view insertSubview:mtable belowSubview:self.mtitle];

}
-(void)viewWillShow
{
    [[ViewManager sharedViewManager] showSuccessProgressHUDWithStatus:@"查询中..."
                                                             withView:self.view];
}

#pragma mark - button selector -
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)nextButtonPressed
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择人群"
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"只看女",@"只看男",@"全部", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];
}
#pragma mark - private fun-
-(void)reloadTable
{
    [mtable reloadData];
}

-(void)refreshView
{
    [mtable reloadData];
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0)
    {
        if ([DataControl shareControl].nearPeopleCurShowArr != nil) {
            return [DataControl shareControl].nearPeopleCurShowArr.count;
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row = [indexPath row];
    
    static NSString *CellIdentifier = @"Cell";
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PersonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    
    switch (section)
    {
        case 0:
        {
            if ([DataControl shareControl].nearPeopleCurShowArr != nil &&
                [DataControl shareControl].nearPeopleCurDic != nil &&
                row < [[DataControl shareControl].nearPeopleCurShowArr count]) {
                NSString *mail = [[DataControl shareControl].nearPeopleCurShowArr objectAtIndex:row];
                
                NSNumber *length = [[DataControl shareControl].nearPeopleCurDic objectForKey:mail ];
                
                FirendInfo *fi = [[DataControl shareControl] SearchInfo:mail ];
                NSString *url = [[DataControl shareControl] SearchHeadImage:mail];
                if (fi != nil)
                {
                    [[ViewManager sharedViewManager] dismissProgressHUD];
                    cell.nameLabel.text = fi.name;
                    if (fi.sex == 1) {
                        cell.sexicon.image = [UIImage imageNamed:@"icon_male.png"];
                    }else{
                        cell.sexicon.image = [UIImage imageNamed:@"icon_female.png"];
                    }
                    if ([url length] > 0)
                    {
                        cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                        UIImage *headImage = [UIImage imageWithContentsOfFile:url];
                        if (headImage) {
                            cell.imageView.image = nil;
                            cell.headImageView.image = [UIImage imageWithContentsOfFile:url];
                        }
                    }else{
                        cell.headImageView.image = nil;
                        cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                    }
                    
                    cell.signLabel.text = fi.strSign;
                    
                }else{
                    cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                }
                
                if (length != nil)
                {
                    NSString *strlen = @"";
                    int ilen = [length intValue];
                    if (ilen > 950) {
                        int km = ilen / 1000;
                        if (km == 0) {
                            strlen = @"1公里内";
                        }
                        else
                        {
                        int m = (ilen % 1000) / 100;
                        
                        if (m == 0 && km <= 3) {
                            strlen = [NSString stringWithFormat:@"%d公里内",km];
                        }
                        else if(km > 3)
                        {
                            strlen = @"大于3公里";
                        }
                            else
                        {
                            strlen = [NSString stringWithFormat:@"%d.%d公里内",km,m];
                        }
                        }
                    }
                    else
                    {
                        int ninterval = ilen / 100;
                        strlen = [NSString stringWithFormat:@"%d米内",100 * (ninterval + 1)];
                    }
                    
                    cell.disLabel.text = strlen;
                }

            }
            
            
        }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            break;
        default:
            break;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [cell changeCellSize];//动态定义控件的坐标
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch ([indexPath section]) {
        case 0:
        {
            if ([DataControl shareControl].nearPeopleCurShowArr != nil)
            {
                int row = [indexPath row];
                if (row >=0 && row < [[DataControl shareControl].nearPeopleCurShowArr count])
                {
                    NSString *mail = [[DataControl shareControl].nearPeopleCurShowArr objectAtIndex:row];
                    FirendInfo *fi = [[DataControl shareControl] SearchInfo:mail];
                    if(fi != nil)
                    {
                        [[DataControl shareControl] setCurrentFI:fi];
                        [[ViewManager sharedViewManager] showView:VI_PERSONINFO
                                                 withIsRemoveCurV:false
                                                    withDirection:VD_RIGHT];
                    }
                }

            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -ActionSheet delegate-

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        nextItem = nil;
        nextItem = [self createBarButtonItemWithTitle:@"筛选(女)"
                                               target:self
                                               action:@selector(nextButtonPressed)];
        self.navItem.rightBarButtonItem = nextItem;
        
        setNearPeopleFilterType(NTF_FEMALE);
        
        if ([[DataControl shareControl] IsNeedVisitNPFamale])
        {
            
            [[DataControl shareControl].LCDS addLogicCommand:LC_NEARPEOPLE];
            
            [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"查询中..."
                                                                     withView:self.view];
            [[LogicControl sharedLogicControl] changeState:GS_WITENEAR];
        }
        else
        {
            [DataControl shareControl].nearPeopleCurShowArr = [DataControl shareControl].nearFemaleShowArr;
            [DataControl shareControl].nearPeopleCurDic = [DataControl shareControl].nearFemaleDic;
            
            [mtable reloadData];
        }
    }
    else if (buttonIndex == 1)
    {
        nextItem = nil;
        nextItem = [self createBarButtonItemWithTitle:@"筛选(男)"
                                               target:self
                                               action:@selector(nextButtonPressed)];
        self.navItem.rightBarButtonItem = nextItem;
        
        setNearPeopleFilterType(NTF_MALE);
        
        if ([[DataControl shareControl] IsNeedVisitNPMale])
        {
            
            [[DataControl shareControl].LCDS addLogicCommand:LC_NEARPEOPLE];
            
            [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"查询中..."
                                                                     withView:self.view];
            [[LogicControl sharedLogicControl] changeState:GS_WITENEAR];
        }
        else
        {
            [DataControl shareControl].nearPeopleCurShowArr = [DataControl shareControl].nearMaleShowArr;
            [DataControl shareControl].nearPeopleCurDic = [DataControl shareControl].nearMaleDic;
            
            [mtable reloadData];
        }
    }
    else if (buttonIndex == 2)
    {
        nextItem = nil;
        nextItem = [self createBarButtonItemWithTitle:@"筛选"
                                               target:self
                                               action:@selector(nextButtonPressed)];
        self.navItem.rightBarButtonItem = nextItem;
        
        setNearPeopleFilterType(NTF_ALL);
        
        if ([[DataControl shareControl] IsNeedVisitNearPeople])
        {
            
            [[DataControl shareControl].LCDS addLogicCommand:LC_NEARPEOPLE];
            
            [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"查询中..."
                                                                     withView:self.view];
            [[LogicControl sharedLogicControl] changeState:GS_WITENEAR];
        }
        else
        {
            [DataControl shareControl].nearPeopleCurShowArr = [DataControl shareControl].nearPeopleShowArr;
            [DataControl shareControl].nearPeopleCurDic = [DataControl shareControl].nearPeopleDistanceDic;
            
            [mtable reloadData];
        }
    }
}

@end
